package FrameWork;

import NetworkReceiver.JobScheduler;
import NetworkSender.ResultScheduler;
import java.util.ArrayList; 

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.E008AE27-8275-8C9F-2637-F9ACC301D9CA]
// </editor-fold> 

/**
 *
 * @author Ahmed Osama
 * this class is responciple for the room operations 
 * and it contain the main controls of the room 
 * it operates only one room at a time 
 * but it can contains backup rooms in the arraylist rooms 
 * this class is singlton class can't be create, but it 
 * can return an instant by calling getInstant property
 * @see Room
 */

public class RoomOperator implements BasicRoom, MainRoom {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.CF788006-F3F5-D162-AD06-F22E72D27296]
    // </editor-fold> 
    private static RoomOperator uniqueInstance = null;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.E69C7272-D671-F9E1-FB3A-284A5FD8A8BD]
    // </editor-fold> 
    /**
     * Main Room Operation
     */
    private Room mainRoom;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.573A5F58-7ABD-6EB2-4309-24B2350F4234]
    // </editor-fold> 
    /**
     * Array of Rooms that can be backup for the exist main room
     */
    private ArrayList<Room> rooms;

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.2B3944EE-479F-0DA0-1D8F-DBA3546B21F9]
    // </editor-fold> 
    private int singletonData = 0;
   
    /**
     * this flag cause the Room Operator to be stop
     */
    public boolean exitFlag = false;

    /**
     * Constants Used to handle class send and receive items
     */
    public static final String EXIST = "EXIST";
    public static final String NOTEXIST = "NOTEXIST";
    public static final String NORETURN = "NORETURN";
    public static final String RETURN   = "RETURN";

    /**
     * check for the exit flag
     * @return if exit signal is raised or not "boolean"
     */
    public boolean isExitFlag() {
        return exitFlag;
    }

    /**
     * it set or reset the exit flag signal
     * @param exitFlag
     */
    public void setExitFlag(boolean exitFlag) {
        this.exitFlag = exitFlag;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.6A07708C-C001-ED24-A1FF-BAC7B0312C35]
    // </editor-fold> 
    
    /**
     * it is the room operator constructor
     */
    private RoomOperator()
    {
        rooms = new ArrayList<Room>();
    }
    
    public static RoomOperator instance () {
      
        if(uniqueInstance == null)
            uniqueInstance = new RoomOperator();
        
        return uniqueInstance;
    }

    public void operate() {
       
        Item item  = checkNewJobs();
        if(item.getState().equals(EXIST))
        {
            item = executeJob(item);
            if(item.getState().equals(RETURN))
            {
                submitResult(item);
            }
        }
        item = checkAdminJobs();
        
        if(item.getState().equals(EXIST))
        {
            item = executeJob(item);
            if(item.getState().equals(RETURN))
            {
                submitResult(item);
            }
        }
     
            
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.9C15CB91-D983-8741-7541-73E70A30A126]
    // </editor-fold> 
    public void setRoom (ArrayList<Room> val) {
        this.rooms = val;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.F42E8FE4-359E-21EE-C31E-553805CED716]
    // </editor-fold> 
    public void singletonOperation () {
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.EFE94688-EBD8-8AAE-0834-6C814A359105]
    // </editor-fold> 
    public int getSingletonData () {
        return 0;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.D087046F-5D59-F623-99A0-0C6FB7481AE1]
    // </editor-fold> 
    public Room getMainRoom () {
        return mainRoom;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.F1811E9A-35AF-4179-E595-A4608B4D78C7]
    // </editor-fold> 
    public void setMainRoom (Room val) {
        this.mainRoom = val;
    }

    public void itemCreatorAction(ItemTranslatorEngine engine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void serviceGatewayAccess(ServiceGateway engine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void userCreatorAction(String userXML, UserCreatorEngine engine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void itemCreatorAction(String itemXML, ItemTranslatorEngine engine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void serviceGatewayAccess(int id, ServiceGateway engine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void roomBuilderAction(String name, int id, RoomCreatorEngine engine) {
      
      String str= id +";" +name;
      Room temp = (Room)engine.addInstance(str); 
     
      if(singletonData == 0)
      {
          mainRoom = temp;
          singletonData++;
          return;
      }
      rooms.add(temp);
    }

    public void deleteRoom(int id, RoomCreatorEngine engine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void modifyRoomParameters(String name, int id, RoomCreatorEngine engine) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private Item checkAdminJobs() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private Item checkNewJobs() {
        
        JobScheduler jobScheduler  = JobScheduler.getInstance();
        if(jobScheduler.hasItems())
        {
            return jobScheduler.deleteItem();
        }
        else
        {
           Item item = new Item();
           item.setState(NOTEXIST);
           return  item;
        }
    }

    private void executeAdminJob() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private Item executeJob(Item item) {
        
        
        
       return new Item();
        
    }

    private void submitResult(Item item) {
        
        ResultScheduler resultScheduler = ResultScheduler.getIntsance();
        resultScheduler.addItem(item);
    }

}

